<template>
  <div class="login">
    <el-card class="box-card">
      <div slot="header" class="clearfix card-title">
        <img src="../../assets/img/logo.png" class="img-logo" alt="" />
        <img src="../../assets/img/cn.png" class="img-cn" alt="" />
      </div>
      <!-- 登录 -->
      <el-form
        :model="ruleForm"
        :rules="rules"
        ref="ruleForm"
        label-width="100px"
        class="demo-ruleForm"
      >
        <el-form-item label="账号:" prop="account">
          <el-input v-model="ruleForm.account"></el-input>
        </el-form-item>
        <el-form-item label="密码:" prop="password">
          <el-input type="password" v-model="ruleForm.password"></el-input>
        </el-form-item>
        <el-form-item>
          <el-button type="primary" @click="submitForm">登录</el-button>
        </el-form-item>
      </el-form>
    </el-card>
  </div>
</template>

<script>
export default {
  name: "loginView",
  data() {
    return {
      ruleForm: {
        account: "",
        password: "",
      },
      rules: {
        account: [
          { required: true, message: "请输入用户名", trigger: "blur" },
          { min: 3, max: 12, message: "长度在 3 到 12个字符", trigger: "blur" },
        ],
        password: [{ required: true, message: "请输入密码", trigger: "blur" }],
      },
    };
  },
  methods: {
    submitForm() {
      this.$refs.ruleForm.validate((valid) => {
        if (valid) {
          // 调用vuex中的action
          this.$store.dispatch("LoginAction", this.ruleForm);
        } else {
          console.log("error submit!!");
          return false;
        }
      });
    },
  },
};
</script>

<style lang="scss" scoped>
.login {
  .box-card {
    width: 400px;
    height: 400px;
    margin: 100px auto;
    .card-title {
      display: flex;
      justify-content: center;
      align-items: center;
      position: relative;
      .img-cn {
        position: absolute;
        top: -20px;
        right: -20px;
        width: 50px;
      }
      .img-logo {
        width: 160px;
      }
    }
  }
}
</style>